package example1_loginLog.dao;

import example1_loginLog.util.MD5Encryption;
import example1_loginLog.vo.LoginLog;
import example1_loginLog.vo.User;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.UUID;

public class LoginLogDao {


    Connection conn = null;
    PreparedStatement ps = null;

    ResultSet rs = null;

    /**
     * 通过用户名和密码查询数据
     * @param username
     * @param password
     * @return
     */
    public User selectUserByUsernameAndPassword(String username,String password){

        User user = null;

        try {
            Class.forName("com.mysql.jdbc.Driver");

            conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/bigdata","root","root");

            ps = conn.prepareStatement("select * from user where username=? and password=?");

            ps.setString(1,username);
            ps.setString(2, MD5Encryption.getEncryption(password));

            System.out.println(username);
            System.out.println(MD5Encryption.getEncryption(password));


            rs = ps.executeQuery();

            if(rs.next()){

                user = new User(
                   rs.getString("id"),
                   rs.getString("name"),
                   rs.getInt("age"),
                   rs.getString("username"),
                   rs.getString("password")
                );
            }

        } catch (Exception e) {
            throw new RuntimeException(e);
        }finally {

            try {
                if(null!=rs){
                    rs.close();
                }

                if(null!=ps){
                    ps.close();
                }

                if(null!=conn){
                    conn.close();
                }
            }catch (Exception e){
                e.printStackTrace();
            }

        }

        return user;

    }

    /**
     * 向登录日志表中添加一条数据
     * @param log
     */
    public int insertLog(LoginLog log){

        //初始化添加日志操作的受影响行数
        int insertLogCount = 0;

        try {
            Class.forName("com.mysql.jdbc.Driver");

            conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/bigdata","root","root");

            ps = conn.prepareStatement("insert into loginlog values(?,?,?,?,?)");

            ps.setString(1, log.getId());
            ps.setString(2, log.getUserId());
            ps.setString(3, log.getOperation());
            ps.setString(4, log.getDateTime());
            ps.setString(5, log.getSessionId());

            insertLogCount = ps.executeUpdate();

        } catch (Exception e) {
            throw new RuntimeException(e);
        }finally {

            try {

                if(null!=ps){
                    ps.close();
                }

                if(null!=conn){
                    conn.close();
                }
            }catch (Exception e){
                e.printStackTrace();
            }

        }
        return insertLogCount;
    }

}
